﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Snake.Client
{
    public static class Screen
    {
        private static GraphicsDeviceManager _graphicsDeviceManager;
        private static SpriteBatch _spriteBatch;
        private static Texture2D _segmentTexture;
        private static Texture2D _backgroundTexture;
        private static Texture2D _foodTexture;
        private static SpriteFont _font;

        public static int Height
        {
            get { return _graphicsDeviceManager.GraphicsDevice.PresentationParameters.BackBufferHeight; }
        }
        public static int Width
        {
            get { return _graphicsDeviceManager.GraphicsDevice.PresentationParameters.BackBufferWidth; }
        }

        public static void CreateGraphicsDevice(GraphicsDeviceManager graphicsDeviceManager)
        {
            _graphicsDeviceManager = graphicsDeviceManager;
        }
        public static void Initialize(int screenWidth, int screenHeight)
        {
            _graphicsDeviceManager.PreferredBackBufferHeight = screenHeight;
            _graphicsDeviceManager.PreferredBackBufferWidth = screenWidth;
            _graphicsDeviceManager.IsFullScreen = true;
            _graphicsDeviceManager.ApplyChanges();
        }
        public static void LoadResourses(Texture2D segmentTexture, Texture2D backgroundTexture, Texture2D foodTexture, SpriteFont font)
        {
            _segmentTexture = segmentTexture;
            _backgroundTexture = backgroundTexture;
            _foodTexture = foodTexture;
            _font = font;

            _spriteBatch = new SpriteBatch(_graphicsDeviceManager.GraphicsDevice);
        }
        public static void BeginDrawing()
        {
            _spriteBatch.Begin();
        }
        public static void DrawBackground()
        {
            _spriteBatch.Draw(_backgroundTexture, new Rectangle(0, 0, Screen.Width, Screen.Height), Color.White);
        }
        public static void DrawFood(Vector2 foodPosition)
        {
            _spriteBatch.Draw(_foodTexture, foodPosition, Color.White);
        }
        public static void DrawSnake(Body snakeBody, Health health)
        {
            byte nonRedValue = (byte) (health.Value * byte.MaxValue);
            Color headColor = new Color(255, nonRedValue, nonRedValue);

            _spriteBatch.Draw(_segmentTexture, snakeBody.Head.Position, headColor);

            for (var i = 1; i < snakeBody.Size; i++)
            {
                _spriteBatch.Draw(_segmentTexture, snakeBody.Segments[i].Position, Color.White);
            }
        }
        public static void DrawPlayerStatus(Player player, int playerIndex)
        {
            string statusString = player.Name + " -> " + player.Points;
            Vector2 statusStringPosition = new Vector2(0, playerIndex * _font.LineSpacing);

            _spriteBatch.DrawString(_font, statusString, statusStringPosition, Color.White);
        }
        public static void DrawDeathScreen()
        {
            string gameOverString = "Game Over";
            string instructionsString = "Press enter to exit :)";
            Vector2 gameOverStringPosition = new Vector2
            (
                Screen.Width / 2 - _font.MeasureString(gameOverString).X / 2,
                Screen.Height / 2
            );
            Vector2 instructionsStringPosition = new Vector2
            (
                Screen.Width / 2 - _font.MeasureString(instructionsString).X/ 2,
                Screen.Height / 2 + _font.LineSpacing
            );

            _spriteBatch.DrawString(_font, gameOverString, gameOverStringPosition, Color.White);
            _spriteBatch.DrawString(_font, instructionsString, instructionsStringPosition, Color.White);
        }
        public static void EndDrawing()
        {
            _spriteBatch.End();
        }
    }
}
